package day10.StreamTest.BufferedStream;

import java.io.*;
import java.util.Scanner;

public class Test2 {

    public static void main(String[] args) throws IOException {
        System.out.print("原文件:");
        String initPath = new Scanner(System.in).nextLine(); //"/Users/hao/Desktop/abc/f3"
        File from = new File(initPath);
        if(!from.isFile()){
            System.out.println("此文件不存在");
            return;
        }

        System.out.print("目标文件:");
        String targetPath = new Scanner(System.in).nextLine();// "/Users/hao/Desktop/abc/f4"
        File to = new File(targetPath);
        if(to.isDirectory()){
            System.out.println("不能是文件夹");
            return;
        }
        try {
            copy(from, to);
            System.out.println("传送完成");
        } catch (Exception e) {
            System.out.println("传送失败");
            e.printStackTrace();
        }

    }

    private static void copy(File from, File to) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(from.getAbsoluteFile()));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(to.getAbsoluteFile()));
        int b;
        while((b = in.read()) != -1){
            out.write(b);
        }
        in.close();
        out.close();
    }
}


